<?php

	class Module_admin_users
	{

		public static function execute()
		{
			Auth::getInstance()->getPermis('admin/users');

			$vis = new Visual();

			$vis->assign('users', Model_Auth::getInstance()->getUsers());

			return $vis->fetch('admin/users.twig');
		}

		public static function add()
		{
			Auth::getInstance()->getPermis('admin/users');

			$vis = new Visual();

			$vis->assign('permis', Model_Auth::getInstance()->getAllPermissions());

			return $vis->fetch('admin/users/edit.twig');
		}

		public static function edit($params)
		{
			Auth::getInstance()->getPermis('admin/users');

			$vis = new Visual();

			$vis->assign('user', Model_Auth::getInstance()->getUser($params['login']));
			$vis->assign('permis', Model_Auth::getInstance()->getAllPermissions());

			return $vis->fetch('admin/users/edit.twig');
		}

		public static function save($params)
		{
			Auth::getInstance(true)->getPermis('admin/users');

			$user = !empty($params['login']) ? Model_Auth::getInstance()->getUser($params['login']) : false;

			if (!$user instanceof OUser) {
				$user        = new OUser();
				$user->login = $params['login'];
			}

			if (!empty($params['password'])) {
				$user->password = $params['password'];
			}

			$user->permis = format($params['permis'], 'ai');

			Model_Auth::getInstance()->setUser($user);

			return true;
		}

		public function delete($params)
		{
			Auth::getInstance()->getPermis('admin/users');

			if (!empty($params['login'])) {
				Model_Auth::getInstance()->delUser($params['login']);
			}

			header('Location: /admin/users/');
			exit;
		}

	}
